const app = getApp()
const WXAPI = require("../../../../api/newApi")
const WXAPIOLD = require("../../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isagree: false, //是否同意
    info: {},
    assetId: '',
    phone: '',
    name: '', //姓名
    imgHttp: app.globalData.imgHttp,
    documentUrl:""//合同地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.phone = wx.getStorageSync('phone')
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom,
      phone: this.data.phone
    })
    this.data.assetId = options.assetId
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getInfo() { //获取卡信息
    var data = {
      assetId: this.data.assetId
    }
    WXAPI.wealDrawInfo(data, (res) => {
      if (res.code == 200) {
        this.setData({
          info: res.result
        })
      }
    })
  },
  agreeClick() {
    this.data.isagree = !this.data.isagree
    this.setData({
      isagree: this.data.isagree
    })
  },
  //查看合同
  handleContract(){
    var data = {
      assetId: this.data.assetId,
    }
    WXAPIOLD.contractInfo(data, (res) => {
      console.log('合同', res)
      const {result} = res
      this.setData({
        documentUrl: result
      })
      this.lookContract()
    })
    
  },
  lookContract(e) {
    // let url = e.currentTarget.dataset.contract
    let url = this.data.documentUrl
    var index = url.lastIndexOf("\.");
    var type = url.substring(index + 1, url.length);
    console.log(this.data.imgHttp + url)
    wx.downloadFile({
      // url: app.globalData.contractHttp,
      url: this.data.imgHttp + url,
      header: {},
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          fileType: type,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
      },
      fail: function (res) {
        console.log('文件下载失败');
        wx.showToast({
          title: '文件打开失败',
          icon: 'none'
        })
      },
      complete: function (res) {},
    })

  },
  receiveClick() { //点击领取
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (!this.data.isagree) {
      wx.showToast({
        title: '请阅读并同意服务协议',
        icon: 'none'
      })
      return
    }
    var data = {
      assetId: this.data.assetId,
      name: this.data.name,
      phone: this.data.phone
    }
    WXAPI.assetCardWealDraw(data, (res) => {
      if (res.code == 200) {
        let detail = JSON.stringify(res.result)
        wx.showToast({
          title: '领取成功',
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '../receiveSuccess/receiveSuccess?detail=' + detail,
          })
        }, 800)
      }
    })
  },
  bindinput(e) {
    console.log(e.detail.value)
    this.data.name = e.detail.value
  },
  jumpAgreement(){
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})